<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Order extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->library('form_validation'); //加载表单验证类库
        $this->load->helper('json');
        $this->load->helper('common');
        $this->load->model('order_model');
        $this->load->model('upload_model');
    }

    /**
     * @SWG\Post(
     *     summary="订单列表接口",
     *     path="/order/list",
     *     tags={"客户端"},
     *     operationId="6",
     *     produces={"application/xml", "application/json"},
     *     @SWG\Parameter(
     *         description="用户身份秘钥",
     *         in="formData",
     *         name="session_key",
     *         required=true,
     *         type="string",
     *     ),
     *     @SWG\Response(
     *         response=200,
     *         description="成功",
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="参数不全",
     *     ),
     * )
     */
    function list() {
        $post = $this->input->post();
        if ($post['session_key']) {
            $list[0] = $this->order_model->getOrderList($post['session_key']);
            $list[1] = $this->order_model->getServerPhone();
            send_json(array('code' => 2000, 'data' => $list, 'msg' => ""));
        } else {
            send_json(array('code' => 4000, 'data' => '', 'msg' => "缺少参数"));
        }
    }

    /**
     * @SWG\Post(
     *     summary="订单详情",
     *     path="/order/detail",
     *     tags={"客户端"},
     *     operationId="5",
     *     produces={"application/xml", "application/json"},
     *     @SWG\Parameter(
     *         description="用户身份秘钥",
     *         in="formData",
     *         name="session_key",
     *         required=true,
     *         type="string",
     *     ),
     *     @SWG\Parameter(
     *         description="订单id",
     *         in="formData",
     *         name="id",
     *         required=true,
     *         type="integer",
     *     ),
     *     @SWG\Response(
     *         response=200,
     *         description="成功",
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="参数不全",
     *     ),
     * )
     */
    public function detail()
    {
        $post = $this->input->post();
        if ($post['id'] && $post['session_key']) {
            $item = $this->order_model->getOrderDetail($post['session_key'], $post['id']);
            if ($item) {
                send_json(array('code' => 2000, 'data' => $item, 'msg' => "订单存在"));
            } else {
                send_json(array('code' => 4004, 'data' => '', 'msg' => "订单不存在"));
            }
        } else {
            send_json(array('code' => 4000, 'data' => '', 'msg' => "缺少参数"));
        }
    }

    /**
     * @SWG\Post(
     *     summary="退款",
     *     path="/order/refund",
     *     tags={"客户端"},
     *     operationId="23",
     *     produces={"application/xml", "application/json"},
     *     @SWG\Parameter(
     *         description="用户身份秘钥",
     *         in="formData",
     *         name="session_key",
     *         required=true,
     *         type="string",
     *     ),
     *     @SWG\Parameter(
     *         description="订单id",
     *         in="formData",
     *         name="id",
     *         required=true,
     *         type="integer",
     *     ),
     *     @SWG\Parameter(
     *         description="退款理由",
     *         in="formData",
     *         name="remark",
     *         required=true,
     *         type="string",
     *     ),
     *     @SWG\Parameter(
     *         description="退款照片1",
     *         in="formData",
     *         name="pic_1",
     *         required=false,
     *         type="string",
     *     ),
     *     @SWG\Parameter(
     *         description="退款照片2",
     *         in="formData",
     *         name="pic_2",
     *         required=false,
     *         type="string",
     *     ),
     *     @SWG\Parameter(
     *         description="退款照片3",
     *         in="formData",
     *         name="pic_3",
     *         required=false,
     *         type="string",
     *     ),
     *     @SWG\Response(
     *         response=200,
     *         description="成功",
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="参数不全",
     *     ),
     * )
     */
    public function refund()
    {
        $post = $this->input->post();
        if ($post['remark'] && $post['session_key'] && $post['id']) {
            $item = $this->order_model->refundOrder($post);
            if ($item) {
                send_json(array('code' => 2000, 'data' => $item, 'msg' => "已提交"));
            } else {
                send_json(array('code' => 4004, 'data' => '', 'msg' => "提交失败"));
            }
        } else {
            send_json(array('code' => 4000, 'data' => '', 'msg' => "缺少参数"));
        }
    }

    /**
     * @SWG\Post(
     *     summary="退款详细信息",
     *     path="/order/refunDetail",
     *     tags={"客户端"},
     *     operationId="24",
     *     produces={"application/xml", "application/json"},
     *     @SWG\Parameter(
     *         description="用户session_key",
     *         in="formData",
     *         name="session_key",
     *         required=true,
     *         type="string",
     *     ),
     *     @SWG\Parameter(
     *         description="订单id",
     *         in="formData",
     *         name="order_id",
     *         required=true,
     *         type="string",
     *     ),
     *     @SWG\Response(
     *         response=200,
     *         description="成功",
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="参数不全",
     *     ),
     * )
     */
    
    public function refunDetail()
    {
        $post = $this->input->post();
        if ($post['order_id'] && $post['session_key']) {
            $item = $this->order_model->getRefunDetail($post);
            if ($item) {
                send_json(array('code' => 2000, 'data' => $item, 'msg' => "存在"));
            } else {
                send_json(array('code' => 4004, 'data' => '', 'msg' => "不存在"));
            }
        } else {
            send_json(array('code' => 4000, 'data' => '', 'msg' => "缺少参数"));
        }
    }

    /**
     * @SWG\Post(
     *     summary="上传图片",
     *     path="/order/uploadData",
     *     tags={"客户端"},
     *     operationId="11",
     *     produces={"application/xml", "application/json"},
     *     @SWG\Response(
     *         response=200,
     *         description="成功",
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="参数不全",
     *     ),
     * )
     */
    public function uploadData()
    {
        $data = $this->upload_model->do_upload('file', "", true, true, "");
        if ($data['status'] == 0) {
            $fileName = $this->config->item("upload_url") . $data['data']['upload_data']['file_name'];
            send_json(array('code' => 2000, 'data' => $fileName, 'file_name' => $data['data']['upload_data']['file_name'], 'msg' => '上传成功'));
        } else {
            send_json(array('code' => 4004, 'data' => $data['data']['error'], 'msg' => '上传失败,请重新上传'));
        }
    }

    //佳品小栈服务协议
    public function agreement()
    {
        $where = array('k' => 'customer_info');
        $info  = $this->db->where($where)->from('app_key_value')->get()->row_array()['v'];
        if ($info) {
            send_json(array('code' => 2000, 'data' => $info, 'msg' => '存在'));
        } else {
            send_json(array('code' => 4004, 'data' => '', 'msg' => '不存在'));
        }
    }
}
